Conditions | 1 |
Paths | 1 |
Total Lines | 141 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | |||
4 | describe('little endiand and big endian reading', function() { |
||
5 | |||
6 | let byteData = require('../../index.js'); |
||
7 | |||
8 | // 16-bit |
||
9 | it('should turn 2 16-bit signed ints to 2 bytes BE (0s)', function() { |
||
10 | assert.deepEqual(byteData.intFrom2Bytes([0, 0, 0, 0], 10, true), |
||
11 | [0, 0]); |
||
12 | }); |
||
13 | it('should turn 2 16-bit signed ints to 2 bytes LE (1s)', function() { |
||
14 | assert.deepEqual(byteData.intFrom2Bytes([1, 0, 1, 0]), |
||
15 | [1, 1]); |
||
16 | }); |
||
17 | it('should turn 2 16-bit signed ints to 2 bytes BE (1s)', function() { |
||
18 | assert.deepEqual(byteData.intFrom2Bytes([0, 1, 0, 1], 10, true), |
||
19 | [1, 1]); |
||
20 | }); |
||
21 | it('should turn 1 16-bit unsigned ints to 2 bytes BE (1s)', function() { |
||
22 | assert.deepEqual(byteData.uIntFrom2Bytes([0, 1], 10, true), |
||
23 | [1]); |
||
24 | }); |
||
25 | it('should turn 2 bytes hex to 1 16-bit float BE (1/3)', function() { |
||
26 | assert.deepEqual(byteData.floatFrom2Bytes( |
||
27 | ["55", "35"], 16, true)[0].toFixed(5), |
||
28 | 0.33325); |
||
29 | }); |
||
30 | |||
31 | // 24-bit |
||
32 | it('should turn 2 24-bit signed ints to 6 bytes BE (0s)', function() { |
||
33 | assert.deepEqual(byteData.intFrom3Bytes([0, 0, 0, 0, 0, 0], 10, true), |
||
34 | [0, 0]); |
||
35 | }); |
||
36 | it('should turn 2 24-bit signed ints to 6 bytes LE (1s)', function() { |
||
37 | assert.deepEqual(byteData.intFrom3Bytes([1, 0, 0, 1, 0, 0]), |
||
38 | [1, 1]); |
||
39 | }); |
||
40 | it('should turn 2 24-bit signed ints to 6 bytes BE (1s)', function() { |
||
41 | assert.deepEqual(byteData.intFrom3Bytes([0, 0, 1, 0, 0, 1], 10, true), |
||
42 | [1, 1]); |
||
43 | }); |
||
44 | it('should turn 1 24-bit unsigned int to 3 bytes BE (8388607)', function() { |
||
45 | assert.deepEqual(byteData.uIntFrom3Bytes(["7f", "ff", "ff"], 16, true), |
||
46 | [8388607]); |
||
47 | }); |
||
48 | it('should turn 2 24-bit signed ints to 6 bytes BE (max range)', function() { |
||
49 | assert.deepEqual(byteData.intFrom3Bytes(["80","00","00", "7f", "ff", "ff"], 16, true), |
||
50 | [-8388608, 8388607]); |
||
51 | }); |
||
52 | it('should turn 2 24-bit signed ints to 6 bytes BE', function() { |
||
53 | assert.deepEqual(byteData.intFrom3Bytes(["80","00","00" , "00","00","01", "7f", "ff", "ff"], 16, true), |
||
54 | [-8388608, 1, 8388607]); |
||
55 | }); |
||
56 | |||
57 | // 32-bit |
||
58 | it('should turn 2 32-bit signed ints to 8 bytes BE (0s)', function() { |
||
59 | assert.deepEqual(byteData.intFrom4Bytes([0, 0, 0, 0, 0, 0, 0, 0], 10, true), |
||
60 | [0, 0]); |
||
61 | }); |
||
62 | it('should turn 2 32-bit signed ints to 8 bytes LE (1s)', function() { |
||
63 | assert.deepEqual(byteData.intFrom4Bytes([1, 0, 0, 0, 1, 0, 0, 0]), |
||
64 | [1, 1]); |
||
65 | }); |
||
66 | it('should turn 2 32-bit signed ints to 8 bytes BE (1s)', function() { |
||
67 | assert.deepEqual(byteData.intFrom4Bytes([0, 0, 0, 1, 0,0, 0, 1], 10, true), |
||
68 | [1, 1]); |
||
69 | }); |
||
70 | it('should turn 1 32-bit signed ints to 4 bytes BE (1s)', function() { |
||
71 | assert.deepEqual(byteData.uIntFrom4Bytes([0, 0, 0, 1], 10, true), |
||
72 | [1]); |
||
73 | }); |
||
74 | it('should turn 8 bytes hex to 2 32-bit ints (max range)', function() { |
||
75 | assert.deepEqual(byteData.intFrom4Bytes( |
||
76 | ["80","0","0","0", "7f","ff","ff","ff"], 16, true), |
||
77 | [-2147483648,2147483647]); |
||
78 | }); |
||
79 | it('should turn 1 32-bit float from 4 bytes BE hex (1s)', function() { |
||
80 | assert.deepEqual(byteData.floatFrom4Bytes( |
||
81 | ["40","9","70","5f"], 16, true)[0].toFixed(7), |
||
82 | 2.1474836); |
||
83 | }); |
||
84 | |||
85 | // 40-bit |
||
86 | it('should turn 2 40-bit signed ints to 10 bytes BE (0s)', function() { |
||
87 | assert.deepEqual(byteData.intFrom5Bytes([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 10, true), |
||
88 | [0, 0]); |
||
89 | }); |
||
90 | it('should turn 2 40-bit signed ints to 10 bytes LE (1s)', function() { |
||
91 | assert.deepEqual(byteData.intFrom5Bytes([1, 0, 0, 0, 0, 1, 0, 0, 0, 0]), |
||
92 | [1, 1]); |
||
93 | }); |
||
94 | it('should turn 2 40-bit signed ints to 10 bytes BE (1s)', function() { |
||
95 | assert.deepEqual(byteData.intFrom5Bytes([0, 0, 0, 0, 1, 0, 0, 0, 0, 1], 10, true), |
||
96 | [1, 1]); |
||
97 | }); |
||
98 | it('should turn 1 40-bit signed ints to 10 bytes LE (1s)', function() { |
||
99 | assert.deepEqual(byteData.intFrom5Bytes( |
||
100 | [1, 0, 0, 0, 0], 10), |
||
101 | [1]); |
||
102 | }); |
||
103 | it('should turn 5 bytes (hex) to 1 unsigned 40-bit int (149515627075)', |
||
104 | function() { |
||
105 | assert.deepEqual(byteData.uIntFrom5Bytes( |
||
106 | ["22","cf","d3","6a","43"], 16, true), |
||
107 | [149515627075]); |
||
108 | }); |
||
109 | |||
110 | // 48-bit |
||
111 | it('should turn 2 48-bit ints to 12 bytes BE (0s)', function() { |
||
112 | assert.deepEqual(byteData.intFrom6Bytes([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 10, true), |
||
113 | [0, 0]); |
||
114 | }); |
||
115 | it('should turn 2 48-bit ints to 12 bytes LE (1s)', function() { |
||
116 | assert.deepEqual(byteData.intFrom6Bytes([1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]), |
||
117 | [1, 1]); |
||
118 | }); |
||
119 | it('should turn 2 48-bit ints to 12 bytes BE (1s)', function() { |
||
120 | assert.deepEqual(byteData.intFrom6Bytes([0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1], 10, true), |
||
121 | [1, 1]); |
||
122 | }); |
||
123 | it('should turn 2 48-bit unsigned ints to 12 bytes BE (1s)', function() { |
||
124 | assert.deepEqual(byteData.uIntFrom6Bytes([0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1], 10, true), |
||
125 | [1, 1]); |
||
126 | }); |
||
127 | it('should turn 1 48-bit ints to 6 bytes hex BE (120637438355317)', function() { |
||
128 | assert.deepEqual(byteData.intFrom6Bytes(["6d", "b8", "17", "a8", "e7", "75"], 16, 2), |
||
129 | [120637438355317]); |
||
130 | }); |
||
131 | it('should turn 1 48-bit unsigned ints to 6 bytes hex BE (120637438355317)', function() { |
||
132 | let bytes = ["6d", "b8", "17", "a8", "e7", "75", |
||
133 | "00", "00", "00", "00", "00", "01", |
||
134 | "00", "00", "00", "00", "00", "01"]; |
||
135 | assert.deepEqual(byteData.intFrom6Bytes(bytes, 16, 2), |
||
136 | [120637438355317, 1, 1]); |
||
137 | }); |
||
138 | |||
139 | // 64-bit |
||
140 | it('should turn 1 64-bit float to 8 bytes BE (pi)', function() { |
||
141 | assert.deepEqual(byteData.floatFrom8Bytes([64, 9, 33, 251, 84, 68, 45, 24], 10, true), |
||
142 | [3.141592653589793]); |
||
143 | }); |
||
144 | }); |
||
145 |